package com.ndp.fb.mq.service.publishhandleservice.pre;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.ec.core.FeedService;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.exception.UnCheckedException;
import com.ndp.fb.mq.api.service.publishhandleservice.PublishPreProcessor;
import com.ndp.fb.rdb.api.ProductFbFeedRdbService;
import com.ndp.fb.rdb.model.FeedFb;
import com.ndp.fb.rdb.model.FeedWe;
import com.ndp.fb.rdb.model.ProductCatalog;
import com.ndp.fb.redis.model.AdPublishCache;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by hunk.qin on 2016/5/10.
 */
@Component
public class PublishPreMaterielCreateImpl implements PublishPreProcessor {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    private ProductFbFeedRdbService productFbFeedRdbService;

    @Reference
    private FeedService feedService;

    @Override
    public String refreshCache(String batchKey, Long batchId,AdPublishCache cache) {
        //build new materiel style all pic
        String result= CommonConstants.TRUE;
        ProductCatalog productCatalog = cache.getProductCatalog();
        feedService.buildMaterialAndUpdateFbFeedUrl(cache.getWeFeedId(),productCatalog.getFbProductCatalogId());
        //build new materiel need long time ，check 1 hour
        int waitCount=0;
        while (true){
            try {
                Thread.sleep(1000*30);
                waitCount++;
                logger.info("wait materiel create complete for all product {0} second.",waitCount*30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            FeedFb feedByCatalog = productFbFeedRdbService.getProductFbFeedByCatalogId(productCatalog.getFbProductCatalogId());
            String fbFeedName=FilenameUtils.getBaseName(feedByCatalog.getReprocessUrl());

            if(fbFeedName.equals(productCatalog.getEcTemplateMaterielId().toString())){
                return  result;
            }
            if(waitCount>120) throw new UnCheckedException("build new material style all product outtime 1hour,please sum of feed");

        }
    }

    @Override
    public boolean check(AdPublishCache cache) {
        ProductCatalog productCatalog = cache.getProductCatalog();
        if(productCatalog==null||productCatalog.getFbProductCatalogId()==null) return false;

        boolean check;
        FeedWe feedWe = feedService.getFeed(cache.getWeFeedId());
        FeedFb feedByCatalog = productFbFeedRdbService.getProductFbFeedByCatalogId(productCatalog.getFbProductCatalogId());
        if(feedByCatalog==null)
            throw new UnCheckedException("query catalog id {},find not fb feed,please check fb_feed table ."+productCatalog.getId());
        if(feedWe.getFeedUrl().equals(feedByCatalog.getReprocessUrl())){
            check=true;
        }else{
            String fbFeedName=FilenameUtils.getBaseName(feedByCatalog.getReprocessUrl());

            if(fbFeedName.equals(productCatalog.getEcTemplateMaterielId().toString())){
                check=false;
                logger.info("catalog materiel id：{},feed url :{}",productCatalog.getEcTemplateMaterielId(),feedByCatalog.getReprocessUrl());
            }
            else{
                logger.error("catalog used materiel id is :"+fbFeedName+", can't apply new materiel id: "+productCatalog.getEcTemplateMaterielId());
                throw new UnCheckedException("catalog used materiel id is :"+fbFeedName+", can't apply new materiel id: "+productCatalog.getEcTemplateMaterielId());
            }
        }
        return check;
    }

}
